<?php
class AttrsController extends ManagerController {
	public function AttrsController(){
		parent::ManagerController();
	}
	
	public function index(){
		$attrs_mod = new AttrsModel();
		$attrs_list = $attrs_mod->find(array('site_id'=>SITE_ID));
		self::assign('attrs', $attrs_list);
		$this->display();
	}
	
	public function edit(){
		if(IS_POST){
			//保存操作
			self::save_attr();
		}
		$module = 'attrs';
		if(isset($_REQUEST['module'])){
			$module = self::get_request_val('module');
		} 
		if(!empty($this->id)){
			//编辑
			$attrs_mod = &m($module);
			$attrs = $attrs_mod->get_by_key($this->id);
			lang_fatch($attrs, LANGUAGE);
			global $types, $relations, $forms;
			
			AttrHelper::format_forms_items($attrs['items']);
			sort_by_rank($attrs['groups']);
			
			if($module === 'forms'){
				//加载列表页显示内容
				$h = new AttrHelper(array('forms'=>&$attrs));
				$items = $h->get_form_items();
				lang_fatch($items, LANGUAGE);
				
				if(isset($attrs['list_view'])){
					foreach ($attrs['list_view'] as $attr_name=>$rank){
						$items[$attr_name]['checked'] = true;
						$items[$attr_name]['rank'] = $rank;
					}
				}
				
				//重新排序
				sort_by_rank($items);
				AttrHelper::format_forms_items($items);
				$this->assign('list_itmes', $items);
			}
			
			$this->assign($module, $attrs);
		}
		//下面主要应用于FormsController的编辑页面
		$attrs_mod = new AttrsModel();
		$attrs_list = $attrs_mod->find(array('site_id'=>SITE_ID), 'name');
		$tree = new Tree($attrs_list);
		$this->assign('attrs_options', $tree->get_options('　'));
		$this->assign('owners', FormsModel::get_form_owner());
		$this->assign('models', MongoModel::get_models());
		$this->display($module . '/edit');
	}
	
	public function copy_attrs(){
		$name = self::get_post_val('name');
		$desc = self::get_post_val('desc');
		if(empty($name) || utf8_strlen($name)>8){
			self::ajax_error('属性名称不正确！');
		}
		if(empty($this->id)){
			self::ajax_error('未指定要复制的分类！');
		}
		$attrs_mod = new AttrsModel();
		$attrs = $attrs_mod->get_by_key($this->id);
		if(empty($attrs)){
			self::ajax_error('未找到要复制的分类');
		}
		$attrs['name'] = $name;
		$attrs['desc'] = $desc;
		unset($attrs['_id']);
		$attrs_mod->insert($attrs);
		self::ajax_info('复制成功！');
	}
	
	public function attr_del(){
		if(empty($this->id)){
			self::ajax_error('未指定要删除的分类！');
		}
		$attrs_mod = new AttrsModel();
		$attrs_mod->delete_by_key($this->id);
		self::ajax_info('删除成功！');
	}
	
	private function save_attr(){
		$name = self::get_post_val('name');
		$desc = self::get_post_val('desc');
		if(empty($name) || utf8_strlen($name)>8){
			self::ajax_error('属性名称不正确！');
		}
		$owner = self::get_post_val('owner', 'user');
		$attrs_mod = new AttrsModel();
		if(empty($this->id)){
			//新建
			$id = gen_guid();
			$attrs_mod->insert(array(
				'_id' => $id,
				'name' => $name,
				'desc' => $desc,
				'site_id' => SITE_ID,
				'owner' => $owner,
			));
			self::ajax_info('创建成功！', $id);
		}else{
			//修改
			$attrs_mod->update_by_key($this->id, array(
				'name' => $name,
				'desc' => $desc,
				'owner' => $owner,
			));
			self::ajax_info('修改成功！', $this->id);
		}
	}
	
	public function item_edit(){
		if(IS_POST){
			self::save_attr_item();
		}
		$code = self::get_val('code');
		$module = 'attrs';
		if(isset($_REQUEST['module'])){
			$module = self::get_request_val('module');
		}

		$item_name = self::get_val('name');
		if(empty($item_name)){
			$item_name = 'items';
		}
		
		$mutil_lan = self::get_int_val('mutil_lan', 1); 
		$attr_mod = &m($module);
		if(!empty($code) && !empty($this->id)){
			$attr_item = $attr_mod->get_by_key($this->id, 'mutil_lan,' . $item_name . '.' . $code);
			
			$item = $attr_item[$item_name][$code];
			$item['code'] = $code;
			$langs = $this->site_info['langs'];
			if($mutil_lan == 0 || (isset($attr_item['mutil_lan']) && !$attr_item['mutil_lan'])){
				$langs = array(LANGUAGE => true);
				$this->assign('site_langs', array(LANGUAGE=>Lang::get_lan_name(LANGUAGE)));
			}
			foreach ($langs as $lang=>$is_default){
				if(isset($item[$lang]['values'])){
					$item[$lang]['values'] = implode("\n", $item[$lang]['values']);
				}
				
			}
			$type = $item['type'];
			if(isset($item[$type])){
				$this->assign('sub_trait', $item[$type]);
			}
			$this->assign('item',  $item);
		}else{
			$attr_item = $attr_mod->get_by_key_site_limit($this->id, 'mutil_lan');
			if($mutil_lan == 0 || (isset($attr_item['mutil_lan']) && !$attr_item['mutil_lan'])){
				$this->assign('site_langs', array(LANGUAGE=>Lang::get_lan_name(LANGUAGE)));
				$this->assign('mutil_lan', false);
			}
		}
		
		$other_properties = array();//其它属性容器
		
		//关联属性
		$relation_mod = new RelationModel();
		$other_properties['relation'] = $relation_mod->get_options(); 
		
		//表单
		$form_mod = new FormsModel();
		$forms = $form_mod->get_site_forms_option(array('site_id'=>SITE_ID), 'name,title');
		$other_properties['forms'] = $forms;
		
		//分类
		$other_properties['cate'] = AttrsModel::get_cate_types();
		
		$this->assign('other_properties', $other_properties);
		
		//分类名称
		$cate_types = AttrsModel::get_cate_types();
		
		$this->assign('id', $this->id);
		$this->assign('module', $module);
		$this->assign('types', AttrsModel::get_attr_types());
		if(isset($_GET['name'])){
			$this->assign('name', $_GET['name']);
		}
		$this->display();
	}
	
	public function item_del(){
		$code = self::get_val('code');
		if(empty($code)){
			self::ajax_error('未指定要删除的属性！');
		}
		if(empty($this->id)){
			self::ajax_error('未指定要删除的属性！');
		}
		$module = 'attrs';
		if(isset($_REQUEST['module'])){
			$module = self::get_request_val('module');
		} 

		$item_name = self::get_val('name');
		if(empty($item_name)){
			$item_name = 'items';
		}
		$attr_mod = &m($module);
		$attr_mod->delete_column_by_key($this->id, $item_name . '.' . $code);
		self::ajax_info('删除成功！');
	}
	
	private function save_attr_item(){
		$code = self::get_post_val('item_code');
		//验证合法性
		if (!preg_match('/[[a-z|A-Z][a-z|A-Z|0-9|_]+/i', $code)) {
			self::ajax_error('代码不合法！');
		}
		$module = 'attrs';
		if(isset($_REQUEST['module'])){
			$module = self::get_request_val('module');
		} 
		$item_null_allow = false;
		if(isset($_POST['item_null_allow'])){
			$item_null_allow = true;
		}
		$mutil_lan = false;
		if(isset($_POST['is_mutil_lan']) && $_POST['is_mutil_lan'] == 1){
			$mutil_lan = true;
		}
		
		$type = self::get_post_val('type');
		$item = array(
			'required' => $item_null_allow,
			'type' => $type,
			'mutil_lan' => $mutil_lan
		);
		
		$mutil_lan = self::get_post_int_val('mutil_lan', 1);
		$attr_mod = &m($module);
		$attr = $attr_mod->get_by_key_site_limit($this->id, 'mutil_lan');
		$langs = $this->site_info['langs'];
		if($mutil_lan === 0 || (isset($attr['mutil_lan']) && !$attr['mutil_lan'])){
			$langs = array(LANGUAGE => true);
		}
		foreach ($langs as $lang=>$is_default){
			$name = self::get_post_val($lang . '_item_name');
			if(empty($name) || utf8_strlen($name)>50){
				self::ajax_error('属性名称['.Lang::get_lan_name($lang).']不正确！');
			}
			$item_default_value = self::get_post_val($lang . '_item_default_value');
			$values = explode("\n", $item_default_value);
			foreach ($values as $k=>$v){
				if(preg_match('/^\s*$/i', $v)){
					unset($values[$k]);
				}
			}
			reset($values);
			$desc = self::get_post_val($lang . '_desc');
			
			$item[$lang] = array(
				'name' => $name,
				'desc' => $desc,
			);
			if(!empty($values)){
				$item[$lang]['values'] = $values;
			}
			
		}
		
		if(empty($this->id)){
			//如果没有指定ID
			$item['code'] = $code;
			self::ajax_info('创建成功', $item);
		}
		$item_name = self::get_post_val('item_name');
		if(empty($item_name)){
			$item_name = 'items';
		}
		
		//验证代码是否存在
		if($attr_mod->exists_column(array('site_id'=>SITE_ID, '_id'=>$this->id), $item_name . '.' . $code)){
			self::ajax_error('代码已经存在：' . $code);
		}
		
		if(isset($_POST[$type . '_trait'])){
			$item['trait'] = self::get_post_val($type . '_trait');
		}
		
		if(isset($_POST[$type])){
			$item[$type] = self::get_post_val($type);
		}
		
		//追加
		$attr_mod->update_by_key($this->id, array($item_name . '.' . $code => $item));
		self::ajax_info('保存成功！');
	}
}

?>